<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;

class IndexController extends Controller
{
    /**
    *   跳转到商城首页
    */
    public function index()
    {
        //商品分类
        // $goodstype = DB::table('goods_type')->where('pid','0')->get();
        
        $news = DB::table('news')->orderBy('id','desc')->take(5)->get();
        //限时特卖
        $timer = DB::table('goods_create')->where('state','2')->skip(0)->take(6)->get();
        return view('web/index',['timer'=>$timer,'news'=>$news]);
    }

    /**
    *   响应首页ajax请求获取热门商品
    */
    public function hotGoods()
    {
        //热卖商品
        $hot = DB::table('goods_create')->where('state','1')->orderBy('clicknum','desc')->skip(0)->take(6)->get();
        if($hot) {
            $hot = json_encode($hot);
            echo $hot;
        } else {
            echo 0;
        }
        
    }

    /**
    *   跳转到商品列表页
    */
    public function goodsList()
    {
        //获取商品总父类id
        $id = $_GET['id'];
        // $goodslist = DB::select("select * from goods_create where state='1' and typeid in(select id from goods_type where path like '0,".$id."%')");

        $goodslist = DB::table('goods_create')
                    ->join('goods_type','goods_type.id','=','goods_create.typeid')
                    ->where('goods_create.state','1')
                    ->where('goods_type.path','like','%0,'.$id.',%')
                    ->select('goods_create.*')
                    ->paginate(4);

        // var_dump($goodslist);
        // var_dump($typelist);exit;
        
        return view('web/goods/brandList',['typeid'=>$id,'goodslist'=>$goodslist]);
    }

    /**
    *   跳转到单一分类商品列表
    */
    public function typeGoodsList()
    {
        //获取单一分类类别id
        $id = $_GET['id'];
        $path = DB::table('goods_type')->where('id',$id)->select('path')->first()['path'];
        //获取总分类id
        $typeid = explode(',',$path)[1];
        $goodslist = DB::table('goods_create')->where('typeid',$id)->paginate(4);

        return view('web/goods/brandList',['goodslist'=>$goodslist,'typeid'=>$typeid]);
    }

    /**
    *   跳转到品牌列表页
    */
    public function brand()
    {
        $brand = DB::table('goods_brand')->get();
        return view('web/goods/brand',['brand'=>$brand]);
    }

    /**
    *   跳转到单一品牌商品列表页
    */
    public function goodsBrand()
    {
        //获取商品品牌id
        $id = $_GET['id'];
        $goodslist = DB::table('goods_create')
                            ->join('goods_brand','goods_brand.id','=','goods_create.brand')
                            ->where('goods_create.brand',$id)
                            ->select('goods_create.*','goods_brand.brand','goods_brand.picbrand')
                            ->get();
        // var_dump($goodslist);exit;
        
        return view('web/goods/brandgoods',['goodslist'=>$goodslist,'brand'=>$id]);
    }

    /**
    *   跳转到商品详情页
    */
    public function product()
    {
        //获取商品id
        $id = $_GET['id'];
        $goodsid = ['goodsid'=>$id];
        $res = DB::table('goods_history')->insert($goodsid);
        if($res) {

            DB::table('goods_create')->where('id',$id)->increment('clicknum',1);
            $detail = DB::table('goods_create')
                    ->join('goods_brand','goods_brand.id','=','goods_create.brand')
                    ->select('goods_create.*','goods_brand.picbrand')
                    ->where('goods_create.id',$id)->get();
            $detail[0]['size'] = explode(',',$detail[0]['size']);
                    // var_dump($detail);exit;
            return view('web/goods/product',['detail'=>$detail[0]]);
        }else {
            return '商品详情获取失败';
        }      
    }

    /**
    *   ajax请求获取浏览历史
    */
    public function goodsHistory()
    {
        $history = DB::table('goods_history')
                    ->join('goods_create','goods_create.id','=','goods_history.goodsid')
                    ->select('goods_create.*')
                    ->orderBy('goods_history.id','desc')
                    ->distinct()
                    ->skip(0)
                    ->take(5)
                    ->get();

        if($history) {
            $history = json_encode($history);
            echo $history;
        } else {
            echo 0;
        }
    }

    /**
    *   ajax请求清空浏览历史
    */
    public function clearHistory()
    {
        $res = DB::table('goods_history')->delete();
        if($res != 0) {
            echo 1;
        } else {
            echo 0;
        }

    }

    /**
    *   限时特卖商品页
    */
    public function sellGoods()
    {
        $sellgoods = DB::table('goods_create')->where('state','2')->get();
        foreach ($sellgoods as $key => $value) {
            $sellgoods[$key]['disc'] = $sellgoods[$key]['price']*(0.8);
            $sellgoods[$key]['diff'] = $sellgoods[$key]['price']*(0.2);
        }
        // var_dump($sellgoods);
        return view('web/goods/sell',['sellgoods'=>$sellgoods]);
    }

    /**
    *   首页限时特卖商品轮播
    */
    public function timerGoods()
    {
        $timergoods = DB::table('goods_create')
                            ->where('state','2')
                            ->orderBy('id','desc')
                            ->skip(0)
                            ->take(5)
                            ->get();
        if($timergoods) {
            $timergoods = json_encode($timergoods);
            echo $timergoods;
        } else {
            echo 0;
        }
    }

    /**
    *   限时特卖商品详情页
    */
    public function sellDetails()
    {
        //获取特卖商品id
        $id = $_GET['id'];
        $timergoods = DB::table('goods_create')->where('id',$id)->get();
        foreach ($timergoods as $key => $value) {
            // $sellgoods[$key]['disc'] = $sellgoods[$key]['price']*(0.8);
            $timergoods[$key]['diff'] = $timergoods[$key]['price']*(0.2);
            $timergoods[$key]['size'] = explode(',',$timergoods[$key]['size']);
        }
        // var_dump($timergoods);exit;
        return view('web/goods/sellDetails',['timergoods'=>$timergoods[0]]);
    }

    /**
    *   商品搜索
    */
    
    public function goodsSearch(Request $request)
    {
        $data = $request->only('search');
        $data = $data['search'];
        
        $res = DB::table('goods_create')
                    ->where('goods','like','%'.$data.'%')
                    ->orWhere('desc','like','%'.$data.'%')
                    ->get();
        if($res) {
            return view('web/goods/category',['goodslist'=>$res]);
        } else {
            return back('没有你要找的商品');
        }
        
    }

    /**
    *   ajax请求获取商品类别
    */
    public function typeList()
    {
        //获取商品总类别id
        $id = $_GET['id']?$_GET['id']:0;
        $typelist = DB::table('goods_type')
                        ->join('goods_create','goods_type.id','=','goods_create.typeid')
                        ->where('goods_type.path','like','%0,'.$id.',%')
                        ->select('goods_type.*')
                        ->distinct()
                        ->get();
        $typelist = json_encode($typelist);
        echo $typelist;
    }

    /**
    *   ajax请求获取所有品牌
    */
    public function allBrands()
    {
        $brands = DB::table('goods_brand')->get();
        if($brands) {
            $brands = json_encode($brands);
            echo $brands;
        } else {
            echo 0;
        }
    }

    /**
    *   ajax请求获取购买量最高的商品
    */
    public function mostBuy()
    {
        $mostbuy = DB::table('goods_create')->orderBy('buynum','desc')->skip(0)->take(5)->get();
        $mostbuy = json_encode($mostbuy);
        echo $mostbuy;
    }

    /**
    *   ajax请求获取热卖商品
    */
    public function feature()
    {
        $data = DB::table('goods_create')->orderBy('buynum','desc')->skip(0)->take(10)->get();
        $data = json_encode($data);
        echo $data;
    }

}
